package com.example.myapplication11_10;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.WindowManager;

import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleDataSet;
import com.github.mikephil.charting.data.BubbleEntry;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.ScatterData;
import com.github.mikephil.charting.data.ScatterDataSet;

import java.util.ArrayList;
import java.util.List;

public class CombinedActivity extends AppCompatActivity {
    private CombinedChart combinedChart;
    private int x= 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_combined);

        combinedChart = findViewById(R.id.chart);
        combinedChart.getDescription().setEnabled(false);
        combinedChart.setDrawBorders(true);

        combinedChart.setDrawOrder(new CombinedChart.DrawOrder[]{
                CombinedChart.DrawOrder.BAR , CombinedChart.DrawOrder.LINE,CombinedChart.DrawOrder.BUBBLE,
                CombinedChart.DrawOrder.CANDLE, CombinedChart.DrawOrder.SCATTER
        });

        CombinedData combinedData = new CombinedData();
        combinedData.setData(getBardata());
        combinedData.setData(getLineData());
        combinedData.setData(getBubbleData());
        combinedData.setData(getCandleData());
        combinedData.setData(getScatterData());
        combinedChart.setData(combinedData);

    }
    private BarData getBardata(){
        List<BarEntry> list1=new ArrayList<>();
        List<BarEntry>list2=new ArrayList<>();
        for (int i=0;i<x;i++){
            list1.add(new BarEntry(i+0.2f,(float) (Math.random()*11f)+3f));
            list2.add(new BarEntry(i+0.5f,(float)(Math.random()*11f)+3f));

        }
        BarDataSet barDataSet = new BarDataSet(list1, "柱状图1");
        barDataSet.setColor(Color.parseColor("#B33333"));
        barDataSet.setValueTextColor(Color.parseColor("#6B88d2"));
        BarDataSet barDataSet1 = new BarDataSet(list2, "柱状图2");
        barDataSet1.setColor(Color.parseColor("#FF2BB0"));
        barDataSet1.setValueTextColor(Color.parseColor("#0288d1"));

        XAxis xAxis=combinedChart.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMaximum(-0.3f);
        xAxis.setAxisMaximum(20-0.3f);

        BarData barData = new BarData();
        barData.addDataSet(barDataSet);
        barData.setBarWidth(0.3f);
        barData.addDataSet(barDataSet1);

        return barData;
    }
    private LineData getLineData(){
        List<Entry>list=new ArrayList<>();
        for(int i=0;i<x;i++){
            list.add(new Entry(i,(float)(Math.random()*11f)+3f));
        }
        LineDataSet lineDataSet = new LineDataSet(list, "折线图");
        lineDataSet.setColor(Color.GRAY);
        lineDataSet.setLineWidth(2f);
        lineDataSet.setHighlightEnabled(false);
        lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        lineDataSet.setDrawCircles(false);
        LineData lineData = new LineData();
        lineData.addDataSet(lineDataSet);

        return lineData;
    }
    private BubbleData getBubbleData(){
        List<BubbleEntry>list=new ArrayList<>();
        for(int i=0;i<x;i++){
            list.add(new BubbleEntry(i+0.7f,(float) (Math.random() * 15f)+0.4f,i));
        }
        BubbleDataSet bubbleDataSet=new BubbleDataSet(list,"泡泡图");
        bubbleDataSet.setColor(Color.YELLOW);
        BubbleData bubbleData=new BubbleData();
        bubbleData.addDataSet(bubbleDataSet);

        return bubbleData;
    }
    private CandleData getCandleData(){
        List<CandleEntry>list=new ArrayList<>();
        for (int i = 0; i < x; i++) {
            float high = 30.0f + ((float) Math.random() * 10f);
            float low = 30.0f - ((float) Math.random() * 10f);
            float diff = high - low;
            float open = high - diff * 0.8f * ((float) Math.random());
            float close = high - diff * 0.8f * ((float) Math.random());
            CandleEntry entry = new CandleEntry(i, high, low, open, close);
            list.add(entry);
        }
        CandleDataSet candleDataSet=new CandleDataSet(list,"K线图");
        candleDataSet.setDecreasingColor(Color.RED);
        candleDataSet.setDecreasingPaintStyle(Paint.Style.FILL);
        candleDataSet.setShadowWidth(1.0f);
        candleDataSet.setIncreasingColor(Color.GREEN);
        candleDataSet.setIncreasingPaintStyle(Paint.Style.FILL);
        candleDataSet.setShadowColor(Color.GRAY);

        CandleData candleData=new CandleData();
        candleData.addDataSet(candleDataSet);

        return candleData;
    }
    private ScatterData getScatterData(){
        List<Entry> list=new ArrayList<>();
        for (int i=0;i<x;i++){
            list.add(new Entry(i+0.2f,(float)(Math.random()*10f)+10f));
        }
        ScatterDataSet scatterDataSet=new ScatterDataSet(list,"散点图");
        scatterDataSet.setColor(Color.GREEN);
        ScatterData scatterData=new ScatterData();
        scatterData.addDataSet(scatterDataSet);

        return scatterData;
    }
}

